<template>
    <div class="page-main DataEntry">
        <MyDialog :model-value="true" :type="DialogType.C1" :title="title" @close="onClose" top="6%" append-to-body>
            <DataTable ref="DataEntryTableRef" :data="list" :hasTop="isChange" :hasAdd="false" :hasFilter="false" :hasSearch="false" :hasCheckbox="false" :hasPager="false" v-bind="$attrs" divHeight="calc(100vh - 300px)">
                <vxe-column title="位置">
                    <template #default="{ row }">
                        <InputNumber v-model="row.position" :precision="2" append="m" maxlength="50" :disabled="!isChange"></InputNumber>
                    </template>
                </vxe-column>

                <vxe-column title="断丝数" v-if="type == Enums.DataEntry_Type.断丝">
                    <template #default="{ row }">
                        <InputNumber v-model="row.num" :precision="0" maxlength="50" :disabled="!isChange"></InputNumber>
                    </template>
                </vxe-column>

                <vxe-column title="测量值" v-if="type == Enums.DataEntry_Type.直径">
                    <template #default="{ row }">
                        <InputNumber v-model="row.num" :precision="2" append="mm" maxlength="50" :disabled="!isChange"></InputNumber>
                    </template>
                </vxe-column>

                <vxe-column title="操作" width="100" v-if="isChange">
                    <template #default="{ row, rowIndex }">
                        <el-button type="danger" @click="onDelete(row, rowIndex)">删除</el-button>
                    </template>
                </vxe-column>

                <!-- 顶部右侧按钮 -->
                <template #rightBtn v-if="isChange">
                    <el-button-group>
                        <el-button v-if="true" type="primary" @click="onAdd">添加</el-button>
                    </el-button-group>
                </template>
            </DataTable>
            <template #footer>
                <el-button type="primary" @click="onSave" v-if="isChange">提交</el-button>
            </template>
        </MyDialog>
    </div>
</template>

<script lang="ts" setup>
import Enums from "@/gx/Enums";
import { DialogType } from "@/_gx/TsEnums";
import Gx from "@/gx/Gx";
import _ from "lodash";

/********************************************** 接收参数 **********************************************/
const usingWireropeID = inject("usingWireropeID") as Ref<number | null>;
const installLengthNum = inject("installLengthNum") as Ref<number | null>;
const props = defineProps({
    //是否为选择组件，在此组件用做弹框选择的子组件时传true
    isChange: {
        type: Boolean,
        default: false,
    },
});

/********************************************** 列表相关定义 **********************************************/

//表格的数据
const list = ref([]);
const title = ref();
const type = ref();
//绳信息
const usingWireropeData = ref();
//绳序号
const wireIndex = ref();

//初始化
const InitDataEntry = (name, dataList, dataType, usingWirerope, dataIndex) => {
    let str = dataType == 1 ? "断丝" : "直径";
    title.value = name ? str + "数据录入 - " + name : str + "数据录入";
    list.value = Gx.DeepCopy(dataList);
    type.value = dataType;
    usingWireropeData.value = usingWirerope;
    wireIndex.value = dataIndex;
};

//分页方法
const onAdd = () => {
    list.value.push({
        type: type.value,
        usingWireropeID: usingWireropeID.value,
        position: null,
        num: null,
    });
};

/********************************************** 列表按钮功能 **********************************************/

//删除
const onDelete = (row, index) => {
    list.value.splice(index, 1);
};

//提交
const onSave = () => {
    for (let i = 0; i < list.value.length; i++) {
        let item = list.value[i];
        if (Gx.IsEmpty(item.position)) {
            App.Tips({ msg: "请完善序号【" + (Number(i) + 1) + "】位置信息！" });
            return;
        }

        if (Number(item.position) > Number(installLengthNum.value)) {
            App.Tips({ msg: "序号【" + (Number(i) + 1) + "】的位置信息必须小于安装长度" + installLengthNum.value + "米！" });
            return;
        }
        if (Gx.IsEmpty(item.num)) {
            if (type.value == Enums.DataEntry_Type.断丝) {
                App.Tips({ msg: "请完善序号【" + (Number(i) + 1) + "】断丝数！" });
            } else {
                App.Tips({ msg: "请完善序号【" + (Number(i) + 1) + "】测量值！" });
            }
            return;
        }
        if (type.value == Enums.DataEntry_Type.断丝) {
            let nextItem = list.value[Number(i) + 1];
            if (Number(i) + 1 < list.value.length) {
                if (Number(item.position) > Number(nextItem.position)) {
                    App.Tips({ msg: "序号【" + (Number(i) + 1) + "】的位置信息必须小于序号【" + (Number(i) + 2) + "】的位置信息！" });
                    return;
                }
                // 判断当前位置+捻距是否超过下一位置
                if (Number(item.position) + Number(usingWireropeData.value.nianJu) / 100 >= Number(nextItem.position)) {
                    item.sum = Number(item.num) + Number(nextItem.num);
                    item.dsEnd = nextItem.position;
                    item.dsStart = item.position;
                } else {
                    item.sum = Number(item.num);
                    item.dsStart = item.position;
                    item.dsEnd = item.position;
                }
            } else {
                item.sum = Number(item.num);
                item.dsStart = item.position;
                item.dsEnd = item.position;
            }
        }
    }

    if (type.value == Enums.DataEntry_Type.断丝) {
        let maxData = _.maxBy(list.value, (o) => {
            return o.sum;
        });
        let positionArr = _.filter(list.value, function (o) {
            return o.sum == maxData.sum;
        });
        emits("close", list.value, positionArr, Gx.IsEmpty(maxData) ? 0 : maxData.sum, type.value, wireIndex.value);
    } else if (type.value == Enums.DataEntry_Type.直径) {
        let minData = _.minBy(list.value, (o) => {
            return o.num;
        });

        let zjDia = ((1 - minData.num / Number(usingWireropeData.value.diameter)) * 100).toFixed(2);
        emits("close", list.value, minData.position, zjDia, type.value, wireIndex.value);
    }
};

//关闭
const onClose = () => {
    emits("close");
};

const emits = defineEmits(["close"]);
defineExpose({ InitDataEntry });
</script>
